﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace DMS
{
    /// <summary>
    /// Summary description for uploadfiles
    /// </summary>
    public class uploadfiles : IHttpHandler
    {
        private readonly System.Web.Script.Serialization.JavaScriptSerializer JsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";

            context.Response.ContentType = "application/json";
            int status = 1;
            string mess = string.Empty;
            string strFileUrl = string.Empty;
            List<string> listFileUrl = new List<string>();
            try
            {
                int catId = context.Request.Params["catId"] == null ? 0 : Convert.ToInt32(context.Request.Params["catId"]);
                int docType = context.Request.Params["docType"] == null ? 0 : Convert.ToInt32(context.Request.Params["docType"]);
                string type = context.Request.Params["type"] == null ? "documents" : context.Request.Params["type"].ToString();
                string strFolderFile = string.Empty;
                if(type=="banner")
                {
                    strFolderFile = "/uploadfiles/" + type + "/" + DateTime.Now.ToString("yyyy/MM/dd") + "/";
                }
                else if (type == "documents")
                {
                    strFolderFile = "/uploadfiles/" + type + "/" + docType + "/" + catId + "/" + DateTime.Now.ToString("yyyy/MM/dd") + "/";
                }
                else
                {
                    strFolderFile = "/uploadfiles/" + docType + "/" + catId + "/" + DateTime.Now.ToString("yyyy/MM/dd") + "/";
                }
                string dirFullPath = HttpContext.Current.Server.MapPath(strFolderFile);
                string[] files;
                int numFiles;
                if (!Directory.Exists(dirFullPath))
                {
                    Directory.CreateDirectory(dirFullPath);
                }
                files = System.IO.Directory.GetFiles(dirFullPath);
                numFiles = files.Length;
                numFiles = numFiles + 1;

                string strFileName = string.Empty;

                foreach (string s in context.Request.Files)
                {
                    HttpPostedFile file = context.Request.Files[s];
                    string fileName = file.FileName;
                    string fileExtension = file.ContentType;

                    if (!string.IsNullOrEmpty(fileName))
                    {
                        //fileExtension = Path.GetExtension(fileName);
                        strFileName = numFiles.ToString() + "_" + file.FileName;
                        strFileUrl = strFolderFile + strFileName;                        
                        string pathToSave = HttpContext.Current.Server.MapPath(strFileUrl);
                        file.SaveAs(pathToSave);
                    }
                }

            }
            catch (Exception ex)
            {
                mess = ex.Message;
                status = 0;
            }
            string json = JsonSerializer.Serialize(new
            {
                Status = status,
                Mess = mess,
                Url = strFileUrl
            });
            //{ Message = messError });
            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}